<?php

namespace App\Models\Promotion;
use App\Models\CheckoutConfig;
use Illuminate\Support\Facades\Redis as Redis;
use App\Models\Promotion\SeckillGoods;
use App\Models\Promotion\SeckillSlot;
use App\Models\Promotion\SeckillAdvert;
use Illuminate\Database\Eloquent\Model;

class Seckill extends Model
{
    protected $table = 'promotion_seckill_master';

    protected $primaryKey = 'promotion_id';

    public $timestamps = false;

    public $incrementing = false;

    public static function getCache($id){

        if (!ebsig_is_int($id)) {
            return [];
        }

        $second_kill_index = 'G_SECOND_KILL_' . $id;

        //查询秒杀缓存
        $second_kill = redisTransformation(Redis::get($second_kill_index));
        if (!empty($second_kill)) {
            return $second_kill;
        }

        //查询秒杀信息
        $second = self::find($id);
        if (!$second) {
            return [];
        }

        //查询秒杀时间段信息
        $slot = SeckillSlot::where(['promotion_id' => $second['promotion_id']])->orderBy('time_slot_id', 'ASC')->get()->toArray();

        $goods = [];
        if ($slot) {
            foreach ($slot as &$s) {

                $time_slot_id = $s['time_slot_id'];

                $goods[$time_slot_id] = [
                    'time_slot_id' => $time_slot_id,
                    'startTime' => $s['startTime'],
                    'endTime' => $s['endTime'],
                    'start_strtotime' => strtotime($second['start_date'] .' '. $s['startTime']),
                    'end_strtotime' => strtotime($second['end_date'] .' '. $s['endTime']),
                    'list' => []
                ];

                //查询秒杀商品信息
                $second_goods = SeckillGoods::where([
                    'promotion_id' => $s['promotion_id'],
                    'time_slot_id' => $time_slot_id
                ])->orderBy('sortOrder', 'ASC')->get()->toArray();

                if ($second_goods) {
                    foreach ($second_goods as &$g) {
                        $postID = $g['postID'];
                        $goods[$time_slot_id]['list'][$postID] = [
                            'time_slot_id' => $g['time_slot_id'],
                            'postID' => $postID,
                            'goodsName' => $g['goodsName'],
                            'salePrice' => $g['salePrice'],
                            'kill_price' => $g['kill_price'],
                            'max_sale_amount' => $g['max_sale_amount'],
                            'limit_count' => $g['limit_count'],
                            'saled_amount' => $g['saled_amount'],
                            'seckill_img' => $g['seckill_img'],
                            'useFlg' => $g['useFlg'],
                            'buy_progress' => 0
                        ];
                    }
                }

            }
        }

        //查询秒杀配置信息
        $config = CheckoutConfig::find($second['checkout_config_id'])->toArray();

        //查询广告位信息
        $advert = SeckillAdvert::where(['expand_id' => $id])->get()->toArray();

        $second_kill = [
            'promotion_id' => $second['promotion_id'],
            'promotion_name' => $second['promotion_name'],
            'start_date' => $second['start_date'],
            'end_date' => $second['end_date'],
            'useFlg' => $second['useFlg'],
            'goods' => $goods,
            'config' => $config,
            'advert_count' => count($advert),
            'advert' => $advert
        ];

        Redis::setex($second_kill_index, 604800, redisTransformation($second_kill, 'set',604800));

        return $second_kill;
    }
}